using System;
using System.Collections.Generic;
using System.Text;

namespace AspWebUtilities.Farm
{
	public class FarmApplication
	{
		static FarmApplicationState application = null;
		static Object lockObject = new object();
		public static FarmApplicationState Application
		{
			get
			{
				if (application == null)
				{
					application = new FarmApplicationState();
				}
				return application;
			}
		}
		public delegate void CodeToRunOnCriticalSection();
		
		public static void RunOnCriticalSection(String[] DataNames, CodeToRunOnCriticalSection CriticalCode)
		{

            lock (lockObject)
            {
                try
                {
                    foreach (String ss in DataNames)
                    {
                        Application.LockForServer(ss);
                    }
                }
                catch (LockedDataException ex)
                {
                    foreach (String ss in DataNames)
                    {
                        Application.UnlockForServer(ss);
                    }
                    throw ex;

                }

                if (CriticalCode != null)
                    CriticalCode();

                foreach (String ss in DataNames)
                {
                    Application.UnlockForServer(ss);
                }
            }
        }
	}
}
